package com.smart.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.smart.common.enums.BooleanEnum;
import com.smart.common.utils.AssertUtil;
import com.smart.context.SessionContext;
import com.smart.model.dtomapper.UnitDtoMapper;
import com.smart.model.domain.UnitDO;
import com.smart.mapper.UnitMapper;
import com.smart.model.request.UnitRequest;
import com.smart.service.UnitService;
import com.smart.model.convert.UnitConvert;
import com.smart.model.vo.UnitVO;
import com.smart.utils.DateUtils;
import com.smart.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;

import static com.smart.utils.PageUtils.getOrderBy;

/**
 * @author zhanghong
 */
@Service
public class UnitServiceImpl extends ServiceImpl<UnitMapper, UnitDO> implements UnitService {
    @Autowired
    private UnitMapper unitMapper;

    @Override
    public UnitVO getByKey(long id) {
        UnitDO unitDO = unitMapper.selectDOById(id);
        UnitVO unitVO = UnitDtoMapper.INSTANCE.do2VO(unitDO);
        return unitVO;
    }

    @Override
    public PageInfo<UnitVO> pageQuery(UnitRequest request, Pageable pageable) {
        PageHelper.startPage(request.getPageNum(),request.getPageSize(),getOrderBy(pageable.getSort())).setReasonable(true);
        request.setProjectId(SessionContext.getProjectId());
        List<UnitDO> list = unitMapper.selectDOListByReq(request);
        List<UnitVO> unitVOList = UnitConvert.buildUnitVoList(list);
        return new PageInfo(unitVOList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(UnitRequest request) {
        request.setProjectId(SessionContext.getProjectId());
        String userPhone = request.getUserPhone();
        String userName = request.getUserName();
        if (!ObjectUtils.isEmpty(userPhone)){
            AssertUtil.assertTrue(StringUtil.isPhoneNumber(userPhone),"负责人手机号输入有误");
        }
        if (!ObjectUtils.isEmpty(userName)){
            AssertUtil.assertTrue(userName.length()<=50,"项目责任人姓名不能超过50字");
        }
        Long projectId = SessionContext.getProjectId();
        UnitDO unitDO = UnitDtoMapper.INSTANCE.request2DO(request);
        unitDO.setCreateUser(projectId);
        unitDO.setDeleted(BooleanEnum.FALSE.getCode());
        unitDO.setCreateTime(DateUtils.getNowDate());
        unitMapper.insertDO(unitDO);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByKey(UnitRequest request) {
        String userPhone = request.getUserPhone();
        String userName = request.getUserName();
        if (!ObjectUtils.isEmpty(userPhone)){
            AssertUtil.assertTrue(StringUtil.isPhoneNumber(userPhone),"负责人手机号输入有误");
        }
        if (!ObjectUtils.isEmpty(userName)){
            AssertUtil.assertTrue(userName.length()<=50,"项目责任人姓名不能超过50字");
        }
        Long projectId = SessionContext.getProjectId();
        UnitDO unitDO = UnitDtoMapper.INSTANCE.request2DO(request);
        unitDO.setUpdateTime(DateUtils.getNowDate());
        unitDO.setUpdateUser(projectId);
        unitMapper.updateDO(unitDO);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteByKey(long id) {
        unitMapper.deleteDOById(id);
        return true;
    }
}
